package com.kanshu.basictest.ui.activity.message;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.kanshu.basictest.R;
import com.kanshu.basictest.model.menu.MenuItem;
import com.kanshu.basictest.ui.base.BaseListMenuActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 验证：
 * 1.处理事件的线程和创建Handler时Looper所属的线程一致。
 * 2.哪个Handler发送消息,哪个Handler处理消息
 */
public class HandlerTestActivity01 extends BaseListMenuActivity implements Handler.Callback {


    public static final String TAG = "HandlerTestActivity01";


    @Override
    protected String provideBarTitle() {
        return "测试1";
    }

    @Override
    protected List<? extends MenuItem> provideMenuData() {
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(0, new MenuItem("sendMsg0", R.color.blue_200));
        menuItems.add(1, new MenuItem("sendMsg1", R.color.blue_200));
        menuItems.add(2, new MenuItem("sendMsg2", R.color.blue_200));
        menuItems.add(3, new MenuItem("sendMsg3", R.color.blue_200));
        menuItems.add(4, new MenuItem("sendMsg4", R.color.blue_200));
        return menuItems;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                sendMsg0();
                break;
            case 1:
                sendMsg1();
                break;
            case 2:
                sendMsg2();
                break;
            case 3:
                sendMsg3();
            case 4:
                sendMsg4();
                break;
            default:
                break;
        }
    }

    private Handler handler0;

    private void sendMsg0() {
        //主线程创建Handler
        handler0 = new Handler(this);
        //消息
        Message msg = Message.obtain(null, 0);
        msg.obj = "sendMsg0";
        //主线程中发送消息
        handler0.sendMessage(msg);

    }


    private Handler handler1;

    private void sendMsg1() {
        //主线程创建Handler
        handler1 = new Handler(this);

        new Thread() {
            @Override
            public void run() {
                super.run();
                //消息
                Message msg = Message.obtain(null, 1);
                msg.obj = "sendMsg1";
                //新线程中发送消息
                handler1.sendMessage(msg);
            }
        }.start();

    }

    private Handler handler2;

    private void sendMsg2() {

        new Thread() {
            @Override
            public void run() {
                super.run();
                //新线程创建Handler
                handler2 = new Handler(HandlerTestActivity01.this);//异常
                //消息
                Message msg = Message.obtain(null, 2);
                msg.obj = "sendMsg2";
                //新线程中发送事件
                handler2.sendMessage(msg);
            }
        }.start();

    }

    private Handler handler3;

    private void sendMsg3() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                //为当前线程创建Looper,以解决sendMsg2()中的异常
                Looper.prepare();
                //新线程创建Handler
                handler3 = new Handler(HandlerTestActivity01.this);//异常
                //消息
                Message msg = Message.obtain(null, 3);
                msg.obj = "sendMsg3";
                //新线程中发送事件
                handler3.sendMessage(msg);
                //开始循环(此步必须手动执行)
                Looper.loop(); //注意：写在Looper.loop()之后的代码不会被执行，这个函数内部应该是一个循环，当调用mHandler.getLooper().quit()后，loop才会中止，其后的代码才能得以运行。
            }
        }.start();

    }

    private Handler handler4;

    private void sendMsg4() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                //新线程创建Handler,但使用主线程的Looper
                handler4 = new Handler(Looper.getMainLooper(), HandlerTestActivity01.this);//异常
                //消息
                Message msg = Message.obtain(null, 4);
                msg.obj = "sendMsg4";
                //新线程中发送事件
                handler4.sendMessage(msg);
            }
        }.start();

    }


    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                Log.i(TAG, "handleMessage: 当前线程：" + Thread.currentThread().getName());//当前线程：main
                Log.i(TAG, "handleMessage: Message.what=" + msg.what + "--Message.obj=" + msg.obj);//Message.what=0--Message.obj=sendMsg0
                Log.i(TAG, "handleMessage:handler0 == msg.getTarget()：" + (handler0 == msg.getTarget()));//handler0 == msg.getTarget()：true
                break;
            case 1:
                Log.i(TAG, "handleMessage: 当前线程：" + Thread.currentThread().getName());//当前线程：main
                Log.i(TAG, "handleMessage: Message.what=" + msg.what + "--Message.obj=" + msg.obj);//Message.what=1--Message.obj=sendMsg1
                Log.i(TAG, "handleMessage:handler1 == msg.getTarget()：" + (handler1 == msg.getTarget()));//handler1 == msg.getTarget()：true
                break;
            case 2:
                Log.i(TAG, "handleMessage: 当前线程：" + Thread.currentThread().getName());//
                Log.i(TAG, "handleMessage: Message.what=" + msg.what + "--Message.obj=" + msg.obj);//java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                Log.i(TAG, "handleMessage:handler2 == msg.getTarget()：" + (handler2 == msg.getTarget()));//
                break;
            case 3:
                Log.i(TAG, "handleMessage: 当前线程：" + Thread.currentThread().getName());//当前线程：Thread-587
                Log.i(TAG, "handleMessage: Message.what=" + msg.what + "--Message.obj=" + msg.obj);//  Message.what=3--Message.obj=sendMsg3
                Log.i(TAG, "handleMessage:handler3 == msg.getTarget()：" + (handler3 == msg.getTarget()));//handler3 == msg.getTarget()：true
                break;
            case 4:
                Log.i(TAG, "handleMessage: 当前线程：" + Thread.currentThread().getName());//当前线程：main
                Log.i(TAG, "handleMessage: Message.what=" + msg.what + "--Message.obj=" + msg.obj);// Message.what=4--Message.obj=sendMsg4
                Log.i(TAG, "handleMessage:handler4 == msg.getTarget()：" + (handler4 == msg.getTarget()));//handler4 == msg.getTarget()：true
                break;
            default:
                break;
        }
        return true;
    }


}
